/**
 * @file make_friends.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_MAKE_FRIENDS != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define MAKE_FRIENDS_FLOWER_LEFT_POS_X (40)
#define MAKE_FRIENDS_FLOWER_LEFT_POS_Y (50)
#define MAKE_FRIENDS_FLOWER_X_OFFSET (25)

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static void make_friends_animation(lv_obj_t * par);
static void make_friends_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * make_friends_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_MAKE_FRIENDS;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = make_friends_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    /*Allocate the object type specific extended data*/
    lv_makefriends_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_makefriends_obj_ext_t));
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->retry_start_tick = lv_tick_get();
#if 0
    //test fake
    ext->retry_timeout = 10000;
#else
    ext->retry_timeout = 120000; /*120 second*/
#endif
    ext->app_query_start_tick = lv_tick_get();
    ext->app_query_period = 3000; /*3 second*/
    ext->status = MAKE_FIRIENDS_NO;

    make_friends_animation(obj);
    ext->status = MAKE_FIRIENDS_RUNNING;

    return obj;
}

void make_friends_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = make_friends_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}


/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_makefriends_obj_ext_t * make_friends_get_ext(lv_obj_t * element)
{
    lv_obj_t * obj = NULL;
    lv_watch_get_parent_obj(element, lv_watch_obj_signal, &obj);
    if(obj == NULL) return NULL;

    lv_makefriends_obj_ext_t * ext = lv_obj_get_ext_attr(obj);
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    return ext;
}

static void make_friends_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#if 1
void app_adapter_get_make_friends_req(void)
{
    //send phone number to app, request to make friends. maybe suppply gps info.
    return;
}

void app_adapter_get_make_friends_cnf(make_friends_status_t * status)
{
    static uint32_t make_friends_fake_cnt = 0;
    *status = MAKE_FIRIENDS_SUCCEED;

    if(make_friends_fake_cnt > 5) {
        *status = MAKE_FIRIENDS_SUCCEED;
    } else {
        *status = MAKE_FIRIENDS_RUNNING;
        make_friends_fake_cnt ++;
    }
}
#endif

static void make_friends_succeed_create(lv_obj_t * obj)
{
    lv_obj_t * img = lv_img_create(obj, NULL);
    LV_ASSERT_MEM(img);
    if(img == NULL) return;

    lv_img_set_src(img, ICON_MAKE_FRIENDS_SUCCEED);
    lv_obj_align(img, obj, LV_ALIGN_CENTER, 0, 0);
}

static void make_friends_failed_create(lv_obj_t * obj)
{
    lv_obj_t * img = lv_img_create(obj, NULL);
    LV_ASSERT_MEM(img);
    if(img == NULL) return;

    lv_img_set_src(img, ICON_MAKE_FRIENDS_FAILED);
    lv_obj_align(img, obj, LV_ALIGN_CENTER, 0, 0);
}

static void make_friends_async_success_del_obj(void *para)
{
    lv_obj_t *obj = para;
    lv_obj_t * watch_obj = NULL;
    lv_watch_get_parent_obj(obj, lv_watch_obj_signal, &watch_obj);
    lv_anim_del(obj, NULL);
    lv_obj_clean(watch_obj);
    make_friends_succeed_create(watch_obj);
}

static void make_friends_async_fail_del_obj(void *para)
{
    lv_obj_t *obj = para;
    lv_obj_t * watch_obj = NULL;
    lv_watch_get_parent_obj(obj, lv_watch_obj_signal, &watch_obj);
    lv_anim_del(obj, NULL);
    lv_obj_clean(watch_obj);
    make_friends_failed_create(watch_obj);
}

static void anim_fp_check_make_friends_status(void * obj, lv_anim_value_t x)
{
    lv_obj_t * watch_obj = NULL;
    lv_watch_get_parent_obj(obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return;

    lv_makefriends_obj_ext_t * ext = make_friends_get_ext(obj);
    if(ext == NULL) return;

    lv_obj_t * img_left = lv_obj_get_child(watch_obj, NULL);

    if(img_left != NULL) lv_obj_set_x(img_left, x);
    lv_obj_t * img_right = lv_obj_get_child_back(watch_obj, NULL);

    if(img_right != NULL) lv_obj_set_x(img_right, lv_obj_get_width(watch_obj) - x - lv_obj_get_width(img_right));

    if(MAKE_FIRIENDS_RUNNING == ext->status) {
        uint32_t msecond = tick_to_ms(lv_tick_elaps(ext->app_query_start_tick));
        if(msecond == 0 || msecond > ext->app_query_period) {
            ext->app_query_start_tick = lv_tick_get();

            app_adapter_get_make_friends_req();
            app_adapter_get_make_friends_cnf(&ext->status);
            if(MAKE_FIRIENDS_SUCCEED == ext->status) {
                lv_async_call(make_friends_async_success_del_obj,obj);
                return;
            } else if(MAKE_FIRIENDS_FAILED == ext->status) {
                lv_async_call(make_friends_async_fail_del_obj,obj);
                return;
            } else {
                msecond = tick_to_ms(lv_tick_elaps(ext->retry_start_tick));
                if(msecond > ext->retry_timeout) {
                    /*Remove the animations from this object*/
                    lv_async_call(make_friends_async_fail_del_obj,obj);
                    return;
                }
            }
        }
    }
}

static void make_friends_animation(lv_obj_t * par)
{
    lv_obj_t * img = lv_img_create(par, NULL);

    lv_img_set_src(img, ICON_MAKE_FRIENDS_LEFT);
    lv_obj_set_pos(img, MAKE_FRIENDS_FLOWER_LEFT_POS_X, MAKE_FRIENDS_FLOWER_LEFT_POS_Y);

    lv_obj_t * label = lv_label_create(par, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_ADD_FRIENDS);
    lv_obj_align(label, par, LV_ALIGN_CENTER, 0, 40);

    img = lv_img_create(par, NULL);

    lv_img_set_src(img, ICON_MAKE_FRIENDS_RIGHT);
    lv_coord_t new_x = lv_obj_get_width(par) - MAKE_FRIENDS_FLOWER_LEFT_POS_X - lv_obj_get_width(img);
    lv_obj_set_pos(img, new_x, MAKE_FRIENDS_FLOWER_LEFT_POS_Y);

    /*Create an animation to move the button continuously left to right*/
    lv_anim_path_t path;

    lv_anim_path_init(&path);

    lv_anim_path_set_cb(&path, lv_anim_path_step);
    lv_anim_t a = {};
    lv_anim_init(&a);
    lv_anim_set_var(&a, img);
    lv_anim_set_values(&a, lv_obj_get_x(img), lv_obj_get_x(img) + MAKE_FRIENDS_FLOWER_X_OFFSET);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)anim_fp_check_make_friends_status);
    lv_anim_set_path(&a, &path);
    lv_anim_set_time(&a, 400);
    lv_anim_set_playback_time(&a, 400);
    lv_anim_set_playback_delay(&a, 0);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 500);

    lv_anim_start(&a);
}

#endif /*USE_LV_WATCH_ADD_FRIENDS*/
